from binance.client import Client
import json
import time
from datetime import datetime
import requests

client = Client()

# 获取订单簿数据
def get_capital_rate_binance(token_array= ['BTCUSDT']):
    result=[]
    for symbol in token_array:
        try_time=5
        while(try_time):
            try:
                url = "https://www.binance.com/fapi/v1/premiumIndex?symbol="+symbol
                response = requests.request("GET", url).json()
                timestamp = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
                result.append({"time":timestamp,"trading_pair":symbol,"platform":"binance","funding":float(response["lastFundingRate"])*100})
                break
            except Exception as e:
                print("binance 获取数据失败 重新获取")
                try_time=try_time-1
                time.sleep(3)
    return result


